<?php

namespace App\Http\Controllers\Front;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Models\Area;
use App\Models\Coupon;
use DB;
use App\Services\Array2XML;

class AddressXMLController extends Controller
{
    /**
     * 创建地址xml文件
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $this->createJson();
    }//end func index()

    /**
     * 生成xml格式的地址数据
     * @return [type] [description]
     */
    public function createXML()
    {

        /*$result = $this->createXML();

        dd($result);*/

        $xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

        $xml .= "<area>\n";

        $area_province = DB::table('area_province')->get();

        if ($area_province) {

            foreach ($area_province as $area_province_key => $area_province_value) {

                /*if ($area_province_key>2) {
                    break;
                }*/

                /*echo '<BR><BR>----------------------------------------------------------------';

                echo '<BR>area_province_key:'.$area_province_key;

                echo '<BR>area_province id:'.$area_province_value->id;

                echo '<BR>area_province code:'.$area_province_value->code;

                echo '<BR>area_province_value:';var_dump($area_province_value);*/

                $xml .= "<province name=\"".$area_province_value->name."\" code=\"".$area_province_value->code."\">\n";

                $area_city = DB::table('area_city')->where('parent_code', $area_province_value->code)->get();

                if ($area_city) {

                    foreach ($area_city as $area_city_key => $area_city_value) {

                        /*echo '<BR><BR>++++++++++++++++++++++++++++++++++++++++++++++++';

                        echo '<BR><BR>area_city_key:'.$area_city_key;

                        echo '<BR>area_city id:'.$area_city_value->id;

                        echo '<BR>area_city code:'.$area_city_value->code;

                        echo '<BR>area_city_value:';var_dump($area_city_value);*/

                        $xml .= "\t<city name=\"".$area_city_value->name."\" code=\"".$area_city_value->code."\">\n";

                        $area_district = DB::table('area_district')->where('parent_code', $area_city_value->code)->get();

                        if ($area_district) {

                            foreach ($area_district as $area_district_key => $area_district_value) {

                                /*echo '<BR><BR>////////////////////////////';

                                echo '<BR><BR>area_district_key:'.$area_district_key;

                                echo '<BR>area_district id:'.$area_district_value->id;

                                echo '<BR>area_district code:'.$area_district_value->code;

                                echo '<BR>area_district_value:';var_dump($area_district_value);*/

                                $xml .= "\t\t<district name=\"".$area_district_value->name."\" code=\"".$area_district_value->code."\"/>\n";

                            }
                        }

                        $xml .= "\t</city>\n";
                    }
                }

            $xml .= "</province>\n";
            }
        }

        $xml .= "</area>\n";

        $fp = fopen('D://area.xml', 'w+');
        // $fp = fopen('D://工作/Afuyigou/area.xml', 'w+');

        fwrite($fp,$xml);

        fclose($fp);

        dd($xml);
        // dd($area_province);

    }

    /**
     * 生成json格式的地址数据
     * @return [type] [description]
     */
    public function createJson()
    {

        $json = "[\n";

        $area_province = DB::table('area_province')->get();

        if ($area_province) {

            $area_province_count = count($area_province) - 1;

            foreach ($area_province as $area_province_key => $area_province_value) {

                /*if ($area_province_key>2) {
                    break;
                }*/

                /*echo '<BR><BR>----------------------------------------------------------------';

                echo '<BR>area_province_key:'.$area_province_key;

                echo '<BR>area_province id:'.$area_province_value->id;

                echo '<BR>area_province code:'.$area_province_value->code;

                echo '<BR>area_province_value:';var_dump($area_province_value);*/

                $json .= "\t{\n";

                $json .= "\t\t\"cities\":[\n";

                $area_city = DB::table('area_city')->where('parent_code', $area_province_value->code)->get();

                if ($area_city) {

                    $area_city_count = count($area_city) - 1;

                    foreach ($area_city as $area_city_key => $area_city_value) {

                        /*echo '<BR><BR>++++++++++++++++++++++++++++++++++++++++++++++++';

                        echo '<BR><BR>area_city_key:'.$area_city_key;

                        echo '<BR>area_city id:'.$area_city_value->id;

                        echo '<BR>area_city code:'.$area_city_value->code;

                        echo '<BR>area_city_value:';var_dump($area_city_value);*/

                        $json .= "\t\t\t{\n";

                        $json .= "\t\t\t\t\"areas\":[\n";

                        $area_district = DB::table('area_district')->where('parent_code', $area_city_value->code)->get();

                        if ($area_district) {

                            $area_district_count = count($area_district) - 1;

                            foreach ($area_district as $area_district_key => $area_district_value) {

                                /*echo '<BR><BR>////////////////////////////';

                                echo '<BR><BR>area_district_key:'.$area_district_key;

                                echo '<BR>area_district id:'.$area_district_value->id;

                                echo '<BR>area_district code:'.$area_district_value->code;

                                echo '<BR>area_district_value:';var_dump($area_district_value);*/

                                $json .= "\t\t\t\t\t{\n";

                                $json .= "\t\t\t\t\t\t\"name\":\"".$area_district_value->name."\",\n";

                                $json .= "\t\t\t\t\t\t\"code\":\"".$area_district_value->code."\"\n";

                                if ($area_district_key == $area_district_count) {

                                    $json .= "\t\t\t\t\t}\n";
                                }else {

                                    $json .= "\t\t\t\t\t},\n";
                                }

                            }
                        }

                        $json .= "\t\t\t\t],\n";

                        $json .= "\t\t\t\t\"city\":\"".$area_city_value->name."\",\n";

                        $json .= "\t\t\t\t\"code\":\"".$area_city_value->code."\"\n";

                        if ($area_city_key == $area_city_count) {

                            $json .= "\t\t\t\t}\n";
                        }else {

                            $json .= "\t\t\t\t},\n";
                        }
                    }
                }

                $json .= "\t\t],\n";

                $json .= "\t\t\"province\":\"".$area_province_value->name."\",\n";

                $json .= "\t\t\"code\":\"".$area_province_value->code."\"\n";

                if ($area_province_key == $area_province_count) {

                    $json .= "\t}\n";
                }else {

                    $json .= "\t},\n";
                }
            }

            $json .= "]\n";
        }

        $fp = fopen('D://area.json', 'w+');

        fwrite($fp,$json);

        fclose($fp);

        dd($json);
        // dd($area_province);

    }


    public function createXMLs()
    {

        $data_array = array(
            array(
            'title' => 'title1',
            'content' => 'content1',
                'pubdate' => '2009-10-11',
            ),
            array(
            'title' => 'title2',
            'content' => 'content2',
            'pubdate' => '2009-11-11',
            )
        );
        $title_size = 1;

        $xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
        $xml .= "<article>\n";

        foreach ($data_array as $data) {
            $xml .= $this->create_item($data['title'], $title_size, $data['content'], $data['pubdate']);
        }

        $xml .= "</article>\n";

        // echo $xml;

        return $xml;

    }

    //  创建XML单项
    function create_item($title_data, $title_size, $content_data, $pubdate_data)
    {

        $item = "<item>\n";
        $item .= "<title size=\"" . $title_size . "\">" . $title_data . "</title>\n";
        $item .= "<content>" . $content_data . "</content>\n";
        $item .= " <pubdate>" . $pubdate_data . "</pubdate>\n";
        $item .= "</item>\n";

        return $item;
    }


    public function Array2XML()
    {

        $request = [
            'province' => [
                'name'=>'河北省',
                'city' => [
                    'name'=>'石家庄市',
                    'district' => '长安区',
                    'district' => '桥东区',
                    'district' => '桥西区'
                ],
                'city' => [
                    'name'=>'秦皇岛市',
                    'district' => '海港区',
                    'district' => '山海关区',
                    'district' => '北戴河区'
                ],
            ],
            'DataInfo' => [
                '@cdata' => 'signedDataInfo'
            ]
        ];

        $requestXml = Array2XML::createXML('Root', $request)->saveXML();
    }
}
